import Vue from 'vue'
import Router from 'vue-router'
import store from '../store'
import Config from '../config/config'

import Home from '@/pages/main/home'

import MasterList from '@/pages/master/masterList'
import MasterDetail from '@/pages/master/masterDetail'

import TaskList from '@/pages/dianping/taskList'
import QuestionRelease from '@/pages/dianping/questionRelease'
import QuestionDetail from '@/pages/dianping/questionDetail'
import MyDianpings from '@/pages/dianping/myDianpings'
import MyQuestionDetail from '@/pages/dianping/myQuestionDetail'
import Answer from '@/pages/dianping/answer'

import PaintingList from '@/pages/painting/paintingList'
import PaintingDetail from '@/pages/painting/paintingDetail'
import PaintingRelease from '@/pages/painting/paintingRelease'
import MyPaintings from '@/pages/painting/myPaintings'

import MyFavorite from '@/pages/me/favorite'

import Login from '@/pages/account/login'
import Register from '@/pages/account/register'
import Bind from '@/pages/account/bind'
import PasswordRetrieve from '@/pages/account/passwordRetrieve'
import PasswordReset from '@/pages/account/passwordReset'
import Agreement from '@/pages/account/agreement'

import Message from '@/pages/main/message'

import Follow from '@/pages/main/follow'

import Me from '@/pages/me/me'
import Setting from '@/pages/me/setting'
import Feedback from '@/pages/me/feedback'
import Wallet from '@/pages/me/wallet'
import Invite from '@/pages/me/invite'
import Follower from '@/pages/me/follower'

import Info from '@/pages/me/setting/info'
import About from '@/pages/me/setting/about'

import Balance from '@/pages/me/wallet/balance'
import Credit from '@/pages/me/wallet/credit'
import Withdraw from '@/pages/me/wallet/withdraw'
import WithdrawApply from '@/pages/me/wallet/withdrawApply'

import qs from 'qs'
import axios from '@/http'

Vue.use(Router)

var router = new Router({
  mode: 'history',
  routes: [
    {
      path: '/home',
      name: 'home',
      component: Home
    },
    {
      path: '/master/list',
      name: 'masterList',
      component: MasterList
    },
    {
      path: '/master/detail/:id',
      name: 'masterDetail',
      component: MasterDetail
    },

    {
      path: '/task/list',
      name: 'taskList',
      component: TaskList
    },
    {
      path: '/question/release',
      name: 'questionRelease',
      component: QuestionRelease,
      meta: {
        title: '发布点评',
        requireAuth: true
      }
    },
    {
      path: '/question/detail/:id',
      name: 'questionDetail',
      component: QuestionDetail
    },
    {
      path: '/dianping/my',
      name: 'myDianpings',
      component: MyDianpings
    },
    {
      path: '/question/detail/my/:id',
      name: 'myQuestionDetail',
      component: MyQuestionDetail
    },
    {
      path: '/question/answer',
      name: 'answer',
      component: Answer,
      meta: {
        title: '回答问题',
        requireAuth: true
      }
    },

    {
      path: '/painting/list',
      name: 'paintingList',
      component: PaintingList
    },
    {
      path: '/painting/detail',
      name: 'paintingDetail',
      component: PaintingDetail,
      meta: {
        title: '作品详情',
        requireAuth: true
      }
    },
    {
      path: '/painting/release',
      name: 'paintingRelease',
      component: PaintingRelease,
      meta: {
        title: '发布作品',
        requireAuth: true
      }
    },
    {
      path: '/painting/my',
      name: 'myPaintings',
      component: MyPaintings,
      meta: {
        title: '我的作品',
        requireAuth: true
      }
    },

    {
      path: '/favorite/my',
      name: 'myFavorite',
      component: MyFavorite,
      meta: {
        title: '我的收藏',
        requireAuth: true
      }
    },

    {
      path: '/login',
      name: 'login',
      component: Login,
      meta: {
        title: '登陆'
      }
    },
    {
      path: '/register',
      name: 'register',
      component: Register,
      meta: {
        title: '注册'
      }
    },
    {
      path: '/bind',
      name: 'bind',
      component: Bind,
      meta: {
        title: '绑定手机',
        requireAuth: true
      }
    },
    {
      path: '/password/reset',
      name: 'passwordReset',
      component: PasswordReset,
      meta: {
        title: '修改密码',
        requireAuth: true
      }
    },
    {
      path: '/password/retrieve',
      name: 'passwordRetrieve',
      component: PasswordRetrieve,
      meta: {
        title: '找回密码'
      }
    },
    {
      path: '/agreement',
      name: 'agreement',
      component: Agreement,
      meta: {
        title: '用户协议'
      }
    },

    {
      path: '/message',
      name: 'message',
      component: Message,
      meta: {
        title: '消息',
        requireAuth: true
      }
    },

    {
      path: '/follow',
      name: 'follow',
      component: Follow,
      meta: {
        title: '关注',
        requireAuth: true
      }
    },

    {
      path: '/me',
      name: 'me',
      component: Me,
      meta: {
        title: '我的',
        requireAuth: true
      }
    },
    {
      path: '/setting',
      name: 'setting',
      component: Setting 
    },
    {
      path: '/feedback',
      name: 'feedback',
      component: Feedback
    },
    {
      path: '/wallet',
      name: 'wallet',
      component: Wallet 
    },
    {
      path: '/follower',
      name: 'follower',
      component: Follower 
    },
    {
      path: '/invite',
      name: 'invite',
      component: Invite
    },
    {
      path: '/setting/about',
      name: 'about',
      component: About
    },
    {
      path: '/setting/info',
      name: 'info',
      component: Info
    },

    {
      path: '/wallet/balance',
      name: 'balance',
      component: Balance
    },
    {
      path: '/wallet/withdraw',
      name: 'withdraw',
      component: Withdraw
    },
    {
      path: '/wallet/withdrawApply',
      name: 'withdrawApply',
      component: WithdrawApply
    },
    {
      path: '*',
      redirect: '/home'
    }

  ]
})

// 定义全局的钩子
router.beforeEach((to, from, next) => {
  // 设置title
  var title = to.meta.title ? to.meta.title : Config.SITE_TITLE
  window.document.title = title
  if (typeof(to.query.invite) != 'undefined') {
    return next({
      path: '/invite',
      params: { inviterId: to.query.inviterId }
    }) 
  }
  if (typeof(to.query.route) != 'undefined') {
    return next({
      path: to.query.route
    })
  }
  if (to.matched.some(r => r.meta.requireAuth)) {
    console.log(store.state)
    if (store.state.isLogin) {
      next()
    } else {
      next({
        path: '/login',
        query: {redirect: to.fullPath}
      })
    }
  } else {
    next()
  }

})

router.afterEach((to, from) => {
  // 判断是否存在网页授权的
  if (to.query.code && to.query.state) {
    var params = {
      'code': to.query.code,
      'state': to.query.state
    }
    if (to.query.state == '0') {
      // base授权
      axios.post('/users/sync_wechat_openid/', qs.stringify(params))
      .then(function(res) {
        console.log(res.data)
        var payload = { 'openId': res.data.openid, 'token': res.data.token }
        store.dispatch('setOpenId', payload)
      })
    } else {
      // 授权获取用户信息
      // base授权
      axios.post('/users/wechat_login/', qs.stringify(params))
      .then(function(res) {
        var payload = { 'userInfo': res.data, 'token': res.data.token, 'openId': res.data.openid  }
        store.dispatch('login', payload)
      })
    }
  }
})

export default router
